/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hibernate;

import com.partaljevic.gambling.Customer;
import com.partaljevic.gambling.Privilege;
import com.partaljevic.gambling.beans.CustomerBean;
import com.partaljevic.tapestry.gambling.service.Customers;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 *
 * @author Nemanja
 */
public class CustomersServiceHibernate implements Customers{

    private final Session session;

    public CustomersServiceHibernate(Session session) {
        this.session = session;
    }

    @Override
    public Customer save(Customer customer) {
        return (Customer)session.get(CustomerBean.class, session.save(customer));
    }

    @Override
    public Collection<Customer> retrieve() {
        Criteria criteria = session.createCriteria(CustomerBean.class);
        return criteria.list();
    }

    @Override
    public Customer retrieve(Serializable id) {
        return (Customer) session.get(CustomerBean.class, id);
    }
    @Override
    public void update(Customer customer){
        session.saveOrUpdate(customer);
    }

    @Override
    public Collection<Customer> retrieveCustomers() {
        List<Customer> list = new LinkedList<Customer>();
        for (Customer customer : retrieve()) {
            if(customer.getPrivilege()== Privilege.CUSTOMER)list.add(customer);
        }
        return list;
    }

    @Override
    public void deleteCustomer(Customer customer) {
        session.delete(customer);
    }

}
